package com.boco.hsqldb.controller;

import com.boco.hsqldb.bean.QueryResult;
import com.boco.hsqldb.mapper.AlarmMapper;
import com.boco.hsqldb.entity.Alarm;
import com.boco.hsqldb.util.DateUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author chenjianxun
 * @version 1.0
 * @date 2023/1/31 10:56
 * @desc hsqldb 性能测试  controller
 */
@RestController
@RequestMapping("/alarm")
public class AlarmController {

    @Resource
    private AlarmMapper alarmMapper;

    @GetMapping(value = "/queryAll")
    public List<Alarm>  query() {
        long start = System.currentTimeMillis();
        List<Alarm> list = alarmMapper.query();
        long end = System.currentTimeMillis();
        String consumeResult = "queryAll耗时:"+(end-start)+"毫秒";
        System.out.println(consumeResult);
        return list;
    }

    @GetMapping(value = "/queryByIndexFp0")
    public QueryResult<List<Alarm>> queryByIndexFp0(@Param("fp0")Integer fp0) {
        long start = System.currentTimeMillis();
        List<Alarm> list = alarmMapper.queryByIndexFp0(fp0);
        long end = System.currentTimeMillis();
        int size = alarmMapper.queryAllSize();
        String consumeResult = "表数据有"+size+"条;  "+"queryByIndexFp0耗时:"+(end-start)+"毫秒";
        System.out.println(consumeResult);
        return new QueryResult<>(consumeResult, list.size(),list);
    }

    @GetMapping(value = "/queryByFp1")
    public QueryResult<List<Alarm>>  queryByFp1(@Param("fp1")Integer fp1) {
        long start = System.currentTimeMillis();
        List<Alarm> list = alarmMapper.queryByFp1(fp1);
        long end = System.currentTimeMillis();
        int size = alarmMapper.queryAllSize();
        String consumeResult = "表数据有"+size+"条;  "+"queryByFp1耗时:"+(end-start)+"毫秒";
        System.out.println(consumeResult);
        return new QueryResult<>(consumeResult,list.size(), list);
    }

    @GetMapping(value = "/queryByCustomSql")
    public QueryResult<List<Alarm>> queryByCustomSql(@Param("sql")String sql) {
        long start = System.currentTimeMillis();
        List<Alarm> result = alarmMapper.queryByCustomSql(sql);
        long end = System.currentTimeMillis();
        String consumeResult = sql+"耗时:"+(end-start)+"毫秒";
        System.out.println(consumeResult);
        return new QueryResult<>(consumeResult,result.size(), result);
    }


    @GetMapping(value = "/addData")
    public String initDatabase(@Param("size")Integer size) {
        long start = System.currentTimeMillis();
        for (int i = 1; i <= size; i++) {
            alarmMapper.add(i, i, 3, 4, DateUtil.formatForDate(new Date()));
        }
        long end = System.currentTimeMillis();
        String consumeResult = "addData耗时:"+(end-start)+"毫秒";
        int result = alarmMapper.queryAllSize();
        return "新增数据执行结果：success;  alarm表目前总条数:"+result+";   "+consumeResult;
    }




}